package cn.sunzq.springdata.elasticsearch.demo.querydemo.service.impl;

import cn.sunzq.springdata.elasticsearch.demo.base.BaseService;
import cn.sunzq.springdata.elasticsearch.demo.base.response.ResponseDTO;
import cn.sunzq.springdata.elasticsearch.demo.constant.IndexMappingConstant;
import cn.sunzq.springdata.elasticsearch.demo.document.DemoDocument;
import cn.sunzq.springdata.elasticsearch.demo.querydemo.enums.ClientTypeEnum;
import cn.sunzq.springdata.elasticsearch.demo.querydemo.service.QueryDemoService;
import cn.sunzq.springdata.elasticsearch.demo.utils.ElasticsearchOperationstUtil;
import cn.sunzq.springdata.elasticsearch.demo.utils.EsRestHighLevelClientUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @创建人 孙中泉
 * @创建时间 2021/7/26
 * @描述
 */
@Service
public class QueryDemoServiceImpl extends BaseService implements QueryDemoService {

    @Autowired
    private EsRestHighLevelClientUtil esRestHighLevelClientUtil;

    @Autowired
    private ElasticsearchOperationstUtil elasticsearchOperationstUtil;

    @Override
    public ResponseDTO<Boolean> indexExist(String indexName, Integer clientType) {
        ClientTypeEnum clientTypeEnum = ClientTypeEnum.getEnumByCode(clientType);
        if(clientTypeEnum != null){
            switch (clientTypeEnum){
                case ElasticsearchOperations:
                    return ResponseDTO.defaultResponse(elasticsearchOperationstUtil.existsIndex(indexName));
                case HighLevelClient:
                    return ResponseDTO.defaultResponse(esRestHighLevelClientUtil.indexExists(indexName));
            }
        }
        return ResponseDTO.defaultResponse(false);
    }

    @Override
    public ResponseDTO<Boolean> createIndex(String indexName, Integer clientType) {
        ClientTypeEnum clientTypeEnum = ClientTypeEnum.getEnumByCode(clientType);
        if(clientTypeEnum != null){
            switch (clientTypeEnum){
                case ElasticsearchOperations:
                    return ResponseDTO.defaultResponse(elasticsearchOperationstUtil.createDocumentRemoveIfExist(indexName, DemoDocument.class));
                case HighLevelClient:
                    return ResponseDTO.defaultResponse(esRestHighLevelClientUtil.createIndexWithJsonString(indexName,IndexMappingConstant.TEST_DEMO_MAPPING));
            }
        }
        return ResponseDTO.defaultResponse(false);
    }

}
